﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using Aspose.Cells;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;



public partial class Report_UserControl_uc_TongHop_XuatNhap_Ton : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();

        }
    }

    private void InitData()
    {

        txtTuNgay.Text = "01/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.Year.ToString();
        txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
        GetPhapNhan();
        GetAllKho("");
    }
    private void GetPhapNhan()
    {
        try
        {
            CateBO cate = new CateBO();
            List<ZD_PRC_PHAPNHAN_GETBY_IDResult> lst = new List<ZD_PRC_PHAPNHAN_GETBY_IDResult>();
            lst = cate.GetPhapNhan("");
            if (lst != null)
            {
                ddlPhapNhan.DataSource = lst;
                ddlPhapNhan.DataTextField = "U_TenPN";
                ddlPhapNhan.DataValueField = "code";
                ddlPhapNhan.DataBind();

                ListItem lstParent = new ListItem("--Chọn--", "");
                ddlPhapNhan.Items.Insert(0, lstParent);
                ddlPhapNhan.SelectedIndex = ddlPhapNhan.Items.IndexOf(lstParent);
            }
        }
        catch
        {
        }
    }
    private void GetAllKho(string id)
    {
        try
        {
            CateRouteBO ct = new CateRouteBO();
            List<DAL.ZD_PRC_V_ZD_GET_LIST_WAREHOUSE_GETBYIDResult> lst = new List<DAL.ZD_PRC_V_ZD_GET_LIST_WAREHOUSE_GETBYIDResult>();
            lst = ct.KhoGet(id);
            if (lst != null)
            {
                ddlKho.DataSource = lst;
                ddlKho.DataTextField = "TENKHO";
                ddlKho.DataValueField = "KHOID";
                ddlKho.DataBind();

                ListItem lstParent = new ListItem("--Tất cả--", "*");
                ddlKho.Items.Insert(0, lstParent);
                ddlKho.SelectedIndex = ddlKho.Items.IndexOf(lstParent);
            }
        }
        catch
        {
        }
    }
    private DataTable ConvertToDataTable<usp_INV_TonghopNXT_WebResult>(List<usp_INV_TonghopNXT_WebResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(usp_INV_TonghopNXT_WebResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (usp_INV_TonghopNXT_WebResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    protected void btnXuat_Click(object sender, EventArgs e)
    {
        DateTime dtTuNgay = new DateTime();
        DateTime dtDenNgay = new DateTime();
        try
        {
            dtTuNgay = DateTime.ParseExact(txtTuNgay.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
            dtDenNgay = DateTime.ParseExact(txtDenNgay.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);

        }
        catch
        {

            lblAlerting.Text = "Bạn chưa nhập ngày hoặc ngày không đúng!";
            return;
        }

        if (ddlPhapNhan.SelectedValue.Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn pháp nhân!";
            return;
        }
        OutReport outRPT = new OutReport();
        //lay database
        ReportBO rpt = new ReportBO();
        List<usp_INV_TonghopNXT_WebResult> result = new List<usp_INV_TonghopNXT_WebResult>();
        result = rpt.Report_TongHop_XuatNhap_Ton(ddlPhapNhan.SelectedValue, ddlKho.SelectedValue, dtTuNgay, dtDenNgay);
        DataTable dt1 = ConvertToDataTable(result);
        DataTable dt2 = new DataTable();
        DataColumn column1 = new DataColumn();
        column1.DataType = System.Type.GetType("System.String");
        column1.ColumnName = "TuNgay";
        dt2.Columns.Add(column1);
        DataColumn column2 = new DataColumn();
        column2.DataType = System.Type.GetType("System.String");
        column2.ColumnName = "DenNgay";
        dt2.Columns.Add(column2);

        DataRow row = dt2.NewRow();
        row["TuNgay"] = dtTuNgay.ToString("dd/MM/yyyy");
        row["DenNgay"] = dtDenNgay.ToString("dd/MM/yyyy");
        dt2.Rows.Add(row);

        DataTable dt3 = new DataTable();
        DataColumn column3 = new DataColumn();
        column3.DataType = System.Type.GetType("System.String");
        column3.ColumnName = "PhapNhan";
        dt3.Columns.Add(column3);

        DataRow row1 = dt3.NewRow();
        row1["PhapNhan"] = ddlPhapNhan.SelectedItem;
        dt3.Rows.Add(row1);
        // datatable 1
        DataTable Report1 = new DataTable();
        // datatable 2
        DataTable Report2 = new DataTable();

        // datatable 3
        DataTable Report3 = new DataTable();
        Report1 = dt1;
        Report2 = dt2;
        Report3 = dt3;
        Report1.TableName = "Report";
        Report2.TableName = "Date";
        Report3.TableName = "Master";
        DataTable[] arrTable = { Report1, Report2, Report3 };
        outRPT.OutReportToScreen(MapPath("~/RPTTemplate/RPT_SUM_NX_TON_TEMPLATE.xls"), arrTable, "_Sum_InOut_Exist.xls");

    }


}
